package com.wechat.api.util;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by Liumh on 2018/3/16.
 */
public class EncoderUtils {

    private Logger logger = LogManager.getLogger(EncoderUtils.class);

    public static String SHA1Encoder(String str){
        if (StringUtils.isBlank(str)){
            return null;
        }
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(str.getBytes());
            byte[] messageDigest = digest.digest();
            StringBuffer hexString = new StringBuffer();
            //字节数组转换为16进制数
            for (int i=0; i<messageDigest.length; i++){
                String shaHex = Integer.toHexString(messageDigest[i]&0xFF);
                if (shaHex.length() < 2){
                    hexString.append("0");
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     *
     * @param encodeStr 加密字符串
     * @param encodeType    加密类型
     * @return
     */
    private String baseEncoder(String encodeStr, String encodeType){
        return null;
    }

    public static void main(String[] args){
        String encoderStr = SHA1Encoder("1111aaabc中国");
        System.out.println(encoderStr);
    }
}
